import { good as GoodApi } from '../../../../../config/request'
module.exports = {
    name: 'shop',
    data() {
        return {
            tableData: [],
            goodForm: {},
            addFormVisible: false,
            updateFlag: false,
            updateIndex: 0,
            name: {
                info: { prop: 'name', label: '商品名称', sortable: true },
                filter: {},
                style: { align: 'center' }
            },
            type: {
                info: { prop: 'type', label: '商品类型', sortable: true },
                filter: {},
                style: { align: 'center' }
            },
            url: {
                info: { prop: 'url', label: '展示图片url', sortable: false },
                filter: {},
                style: { align: 'center' }
            },
            remark: {
                info: { prop: 'remark', label: '备注', sortable: false },
                filter: {},
                style: { align: 'center' }
            },
            status: {
                info: { prop: 'status', label: '状态', sortable: true },
                filter: {},
                style: { align: 'center' }
            },
            intergral: {
                info: { prop: 'intergral', label: '扣除积分', sortable: true },
                filter: {},
                style: { align: 'center' }
            },
            number: {
                info: { prop: 'number', label: '商品数量', sortable: true },
                filter: {},
                style: { align: 'center' }
            },
        }
    },
    methods: {
        onAddGood() {
            this.updateFlag = false;
            this.goodForm = {
                name: '',
                type: 1,
                intergral: '',
                remark: '',
                url: '',
                number: ''
            };
            if (this.$refs['goodForm']) {
                this.$refs['goodForm'].resetFields();
            }
            this.addFormVisible = true;
        },
        onUpdateGood(good, index) {
            this.updateFlag = true;
            this.updateIndex = index;
            this.goodForm = {
                name: good.name,
                type: good.type,
                intergral: good.intergral,
                remark: good.remark,
                url: good.url,
                number: good.number,
                id: good.id
            }
            this.addFormVisible = true;
        },
        /** 提交表单 */
        submitForm(formName) {
            var good = this[formName];
            good.number = Number(good.number);
            good.intergral = Number(good.intergral)
            var that = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //添加问题
                    if (that.updateFlag) {
                        this._updateGood(good);
                    } else {
                        this._createGood(good);
                    }
                } else {
                    return false;
                }
            });
        },
        _updateGood(good) {
            GoodApi.updateGood.call(this, good,
                //添加成功
                (data) => {
                    const newGood = data['good'];
                    Object.assign(newGood, { checked: newGood.status == 1 });
                    this.tableData.splice(this.updateIndex, 1, newGood);
                    this.$message.success('修改商品成功 ！！');
                    this.addFormVisible = false;
                    this.$refs['goodForm'].resetFields();
                },
                //添加失败
                (msg) => {
                    this.$message.error('添加商品失败 ！！');
                });
        },
        _createGood(good) {
            GoodApi.createGood.call(this, good,
                //添加成功
                (data) => {
                    const newGood = data['good'];
                    Object.assign(newGood, { checked: newGood.status == 1 });
                    this.tableData.splice(0, 0, newGood);
                    this.$message.success('添加商品成功 ！！');
                    this.addFormVisible = false;
                    this.$refs['goodForm'].resetFields();
                },
                //添加失败
                (msg) => {
                    this.$message.error('添加商品失败 ！！');
                });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        checkLevel() {
            var user = this.$store.state.user.userinfo;
            // console.log('user', user);
            return user && user.level == 1;
        },
        goodTypes() {
            return [
                { value: 1, label: '抽奖' },
                { value: 2, label: '兑换' },
            ];
        },
        formatterType(item) {
            switch (item.type) {
                case 1:
                    return '抽奖';
                case 2:
                    return '兑换';
            }
            return '一般商品';
        },
        formatterStatus(item) {
            return Number(item.status) == 1 ? '启用' : '禁用';
        },
        _getAllGoods() {
            GoodApi.getAllGoods.call(this, data => {
                this.tableData = data['goods'];
                this.tableData.forEach(good => {
                    good.checked = good.status == 1;
                })
            })
        },
        onCheckedChange(good) {
            GoodApi.updateStatus.call(this, {
                id: good.id,
                status: good.checked ? 1 : 0
            }, data => {
                good.status = data['good'].status;
                good.checked = data['good'].status == 1;
                this.$message.success('该商品已' + (good.checked ? '启用' : '禁用'));
            })
        },
        onDeleteGood(good, index) {
            this.$confirm('是否确认删除该题目？', '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(_ => {
                this.tableData.splice(index, 1);
                GoodApi.deleteGood.call(this, {
                    id: good.id,
                }, data => {
                    this.$message.success('删除商品成功');
                })
            }).catch(_ => {});

        }
    },
    created: function() {
        this._getAllGoods();
    }

}